<template>
	<view class="my-trip">
		<ul>
			<li v-for="(item,index) in tripList" :key="index">
				<dl>

					<dd>
						<p>{{item.title}}</p>
						<text v-if="item.sdate">{{item.sdate}} to {{item.edate}}</text>
						<text> {{item.persons}}people</text>
					</dd>
					<dt>
						<image :src="item.logo" v-if="item.logo" mode="aspectFill"></image>
					</dt>
				</dl>
				<view class="trip_ul">
					<view class="trip_item row between">
						<text>Landlord's name</text>
						<p>{{item.landlords_name}}</p>
					</view>
					<view class="trip_item row between">
						<text>Landlord's phone number</text>
						<p>{{item.landlords_phone}}</p>
					</view>
					<view class="trip_item row between">
						<text>Original price</text>
						<p>EGP{{item.origin_money||'0'}}</p>
					</view>
					<view class="trip_item row between">
						<text>Total date</text>
						<p>{{item.days||'0'}}days</p>
					</view>
					<view class="trip_item row between">
						<text>Service charge</text>
						<p>+EGP{{item.service_charge||'0'}}</p>
					</view>
					<view class="trip_item row between">
						<text>Discount</text>
						<p>-EGP{{item.discount||'0'}}</p>
					</view>
					<view class="trip_item row between">
						<text>{{$t('coupon.text')}}</text>
						<p style="color: #FF0000;">-EGP{{item.coupon_money||'0'}}</p>
					</view>
					<view class="trip_item row between">
						<text>Total amount</text>
						<p style="color: #FF0000;">EGP{{item.total_amount||'0'}}</p>
					</view>
					<view class="trip_item row between">
						<text>pay time</text>
						<p style="color: #999999;">{{item.pay_time||''}}</p>
					</view>
				</view>
				<view class="trip_state row">
					<text v-if="item.state==0" style="color: #ACACAC;">Cancelled</text>
					<text v-if="item.state==1" style="color: #13B335;">Booked</text>
					<text v-if="item.state==2" style="color: #FF3C15;">In Progress</text>
					<text v-if="item.state==3" style="color: #000000;">{{ $t('text.complete') }}</text>
					<text v-if="item.state==4" style="color: orangered;">{{ $t('text.comment') }}</text>
					<button v-if="item.state==1" @click.stop="onCancel(item)">{{$t('text.cancel')}}</button>
					<button v-if="item.state==3" @click.stop="onEval(item)">{{ $t('text.comment') }}</button>
				</view>
			</li>
		</ul>
		<view class="empty" v-if="tripList.length<=0">
			<image src="@/static/list.png" mode=""></image>
			<p>No itinerary available at the moment</p>
		</view>
		<view class="pay_btn">
			<button @click.stop="handleBack">Homepage</button>
		</view>
		<u-loadmore v-if="hasMore&&tripList.length>0" :status="loadStatus" />
		<u-popup :show="isShow" mode="center" round="10" :closeable="true" @close="fullClose">
			<view class="tripWrap">
				<image :src="info.logo" mode="aspectFill"></image>
				<p>{{info.title}}</p>
				<text>{{info.sdate}} to {{info.edate}}</text>
				<view class="phoneFooter">
					<button @click.stop="fullOpen">CONFIRM</button>
					<button @click.stop="fullClose">CLEAR</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getMyReserve,
		cancelReserve
	} from '@/lib/service';
	import {
		showFailure
	} from '@/lib/helper';
	export default {
		data() {
			return {
				loadStatus: 'loadmore',
				pageNo: 1,
				pageSize: 10,
				hasMore: false,
				tripList: [],
				isShow: false,
				reserveid: "",
				info: {
					logo: "",
					title: "",
					sdate: "",
					edate: ""
				},
			}
		},
		onShow() {
			let _this = this;
			_this.pageNo = 1;
			_this.tripList = [];
			_this.loadData();

		},
		onPullDownRefresh() {
			this.pageNo = 1;
			this.tripList = [];
			this.loadData(true);
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1500)
		},
		onReachBottom() {
			if (this.hasMore) {
				this.pageNo = this.pageNo + 1;
				this.loadData();
			}
		},
		methods: {
			loadData(isReset) {
				this.loadStatus = 'loading';
				let obj = {
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}
				getMyReserve(obj).then((res) => {
						uni.hideLoading();
						this.loadStatus = 'no-more';
						this.hasMore = res.data.hasMore;
						if (isReset) {
							this.tripList = res.data.records;
						} else {
							this.tripList = this.tripList.concat(res.data.records);
						}
					})
					.catch((res) => {
						this.loadStatus = 'no-more';
						uni.hideLoading();
						showFailure();
					});
			},
			fullClose() {
				this.isShow = false;
			},
			handleBack() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			onCancel(item) {
				this.reserveid = item.reserveid;
				this.info = item;
				this.isShow = true;

			},
			onEval(item) {
				uni.navigateTo({
					url: '/pages/review/review?reserveid=' + item.reserveid
				})
			},
			fullOpen() {
				let _this = this;
				let obj = {
					reserveid: String(_this.reserveid)
				}
				cancelReserve(obj).then((res) => {
						if (res.statusCode == 0) {
							_this.isShow = false;
							_this.pageNo = 1;
							_this.tripList = [];
							_this.loadData();
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000,
							})
						}

					})
					.catch((res) => {
						showFailure();
					})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pay_btn {
		padding:28rpx 0;
		width: 100%;
		position: fixed;
		bottom: 00;
		left: 0;
		background: #ffffff;

		button {
			width: 630rpx;
			height: 90rpx;
			background: #4980F7;
			border-radius: 45rpx;
			font-weight: 400;
			font-size: 32rpx;
			color: #FEFEFE;
			line-height: 90rpx;
		}
	}

	.my-trip {
		width: 100%;
		min-height: 100%;
		background: #F5F5F5;
		position: absolute;
		padding-bottom: 120rpx;
	}

	.empty {
		text-align: center;
		margin-top: 15vh;

		image {
			width: 376rpx;
			height: 376rpx;
		}

		p {
			font-size: 28rpx;
			color: #999999;
		}
	}

	ul {
		padding: 30rpx;

		li {
			background: #ffffff;
			border-radius: 10rpx;
			padding: 20rpx;
			position: relative;
			margin-bottom: 24rpx;

			dl {
				display: flex;
				justify-content: space-between;
				border-bottom: 0.5px solid #E1E1E1;
				padding-bottom: 14rpx;

				dt {
					image {
						width: 249rpx;
						height: 188rpx;
						background: #eeeeee;
						border-radius: 12rpx;
					}
				}

				dd {
					padding-right: 16rpx;

					p {
						font-family: Microsoft YaHei UI;
						font-weight: bold;
						font-size: 26rpx;
						color: #333333;
					}

					text {
						font-family: Microsoft YaHei UI;
						font-weight: 400;
						font-size: 26rpx;
						color: #666666;
						display: block;
						margin-top: 16rpx;
					}
				}
			}

			.trip_ul {
				padding-bottom: 20rpx;
				border-bottom: 0.5px solid #E1E1E1;

				.trip_item {
					margin-top: 20rpx;
					font-size: 26rpx;

					text {
						display: inline-block;
						color: #999999;
					}
				}
			}

			.trip_state {
				margin-top: 20rpx;
				justify-content: space-between;

				text {
					color: #13B335;
					font-weight: 400;
					font-size: 26rpx;
				}

				button {
					margin: 0;
					// width: 108rpx;
					height: 48rpx;
					background: #FF3C15;
					border-radius: 10rpx;
					font-family: Microsoft YaHei UI;
					font-weight: 400;
					font-size: 26rpx;
					color: #FFFFFF;
					padding: 0 10rpx;
					line-height: 48rpx;
				}
			}
		}
	}

	.tripWrap {
		width: 600rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		text-align: center;

		image {
			width: 249rpx;
			height: 188rpx;
			background: #eeeeee;
			border-radius: 12rpx;
			margin-top: 40rpx;
			margin-bottom: 26rpx;
		}

		p {
			font-family: Microsoft YaHei UI;
			font-weight: bold;
			font-size: 26rpx;
			color: #333333;
		}

		text {
			display: block;
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			font-size: 26rpx;
			color: #333333;
			line-height: 31rpx;
			margin-top: 20rpx;
		}

		.phoneFooter {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 70rpx;
			font-family: Microsoft YaHei UI;
			font-weight: 400;
			border-top: 1px solid #f6f6f6;

			button {
				width: 50%;
				height: 80rpx;
				background: none;
				border: none;
				outline: none;
				line-height: 80rpx;
				color: #333333;
				font-size: 26rpx;
			}

			button:after {
				border: none;
				outline: none;
			}

			button:last-child {
				color: #FF3C15;
				border-left: 1px solid #f6f6f6;
			}
		}
	}
</style>